The goal of sccthemes is to provide a consistent styling framework and helper functions for graphics made in ggplot at the Suffolk County Council.
This package has been heavily inspired by bbplot, the BBC’s R package for creating and exporting graphics made in ggplot.
You can install the development version of sccthemes from GitHub with:
# install.packages("devtools")
devtools::install_github("thomaszwagerman/sccthemes")You can find the full SCC Cookbook. This is a work in progress.
The font and colour codes have been sourced from the Suffolk County Council’s website, and Google Chrome’s Inspect functionality (Ctrl + Shift + i).
The style can be called to any ggplot2 object using
scc_style()
library(ggplot2)
library(gapminder)
library(dplyr)
library(sccthemes)
barchart_data <- gapminder |>
filter(year == 2007 & continent == "Europe") |>
arrange(desc(gdpPercap)) |>
head(5)
barchart_data |>
ggplot(aes(x = country, y = gdpPercap)) +
geom_bar(
stat = "identity",
position = "identity",
color = "#333333",
fill = "#428bca"
) +
geom_hline(
yintercept = 0,
size = 1,
colour = "#333333"
) +
scc_style() +
labs(
title = "Europe's Richest Countries",
subtitle = "GDP per Capita, 2007"
)
Helper functions for certain types of plots are also provided in this repo.
Instead of the code above for instance, this package also contains helper functions which are rough and ready:
library(ggplot2)
library(gapminder)
library(dplyr)
library(sccthemes)
barchart_data <- gapminder |>
filter(year == 2007 & continent == "Europe") |>
arrange(desc(gdpPercap)) |>
head(5)
scc_barchart(
barchart_data,
x = "country",
y = "gdpPercap",
title = "Europe's Richest Countries",
subtitle = "GDP per Capita, 2007"
)
Currently, there are helper functions for the following plot types:
The BBC’s “ggplot cookbook” is a good starting point to use as a reference.
More helper functions will be added as I need to make them.
To publish your work, so will need to save your plot. Saving plots in
ggplot2 can be a bit of a hassle, so we’re porting bbplot’s
finalise_plot() function. Some adjustments have been made,
for example we might not want a logo on every plot. Also, because this
package does not just deal with ggplot, I’ve renamed the function
finalise_ggplot(), to specify this is for
ggplot2 object only.
Once you have created your plot and are happy with it, you can use
finalise_ggplot() so that you can look at it outside
RStudio. Note that the position of the text and other elements do not
render accurately in the RStudio Plots panel, so saving it out and
opening up the files give you an accurate representation of how the
graphic looks.
Also you might want to use the same plot for different purpose: the
size of a plot in a presentation will need to be a different size from
one in a report. finalise_ggplot() allows for this without
having to change the original plot.
library(ggplot2)
library(gapminder)
library(dplyr)
library(sccthemes)
barchart_data <- gapminder |>
filter(year == 2007 & continent == "Europe") |>
arrange(desc(gdpPercap)) |>
head(5)
plot_to_save <- scc_barchart(
barchart_data,
x = "country",
y = "gdpPercap",
title = "Europe's Richest Countries",
subtitle = "GDP per Capita, 2007"
)
finalise_ggplot(
plot_to_save,
source = "Jennifer Bryan, 2017, Gapminder R package, https://github.com/jennybc/gapminder",
save_filepath = "inst/plots/publish_plot_example.png",
width_pixels = 225,
height_pixels = 165
)
The goal of colour palettes is to improve storytelling with data, whether it is to show continuous change in a value, differences between distinct categories or to highlight a specific category that you want to highlight.
Some questions we need to think about when choosing colour:
The Suffolk County Council has its own colour schemes, the recognisable blues and orange found on the website.
This blue (#2d6ca2) is used as the primary colour in graphs, with orange (#e8850c) as the secondary colour. Where applicable, the light blue (#e2eefa) can used as a tertiary colour - but note that this not easily visible against white background (consider using a black outline).
Blue and Orange are colours that are distinct for most common forms of colour blindness/color vision deficiency. However, different types of graphs have vastly different requirements to tell a story, and where the number of categories exceeds three colours the SCC palette is not sufficient.
Therefore, instead of designing our own colour palette, I’d recommend the use of viridis, for graphs with more than three categories. This package has visually appealing colour palettes, and improve graph readability for readers with common forms of color blindness and/or color vision deficiency. Note that for some specific forms, you may have to choose another palette within viridis (for example seaborn, or turbo).
library(sccthemes)
library(gapminder)
library(dplyr)
asia_pop <- gapminder |>
filter(year == 1997 & continent == "Asia") |>
select(country, lifeExp, pop, gdpPercap) |>
top_n(5)
asia_pop$country <- forcats::fct_drop(asia_pop$country)
scc_piechart(
asia_pop,
asia_pop$gdpPercap,
asia_pop$country,
title = "GDP per Capita contribution percentage by county",
subtitle = "Five largest countries only"
)
If there is a further need in the future, other styling options can be added.
For example: